home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 7
/
Apprentice-Release7.iso
/
Environments
/
PowerFantasm™ 4.19a
/
Beginners guide
/
GUIDE EXAMPLES
/
IO_LIB.S
< prev
Wrap
Text File
|
1997-04-01
|
11KB
|
513 lines
*********************************************************************************
*IO LIB.S - GENERAL PURPOSE IO ROUTINES FOR MACINTOSH - THIS VERSION 1.02 *
* *
*VERSION 1.01 - GENERAL USEAGE. *
*VERSION 1.02 - CHANGED SINGLE QUOTES TO DOUBLE QUOTES AT LINE 420 *
*last modified 200195 *
*********************************************************************************
*INCLUDED:
*INIT MAC - INITIALISES MAC AND CURSOR COORDS
*INIT_CURS - RESET CURSOR TO 10,10
*OPEN WINDOW - OPENS A BIG MAC WINDOW - NEEDS TITLE IN A0, RETURNS HANDLE IN A0
*CLOSE_WINDOW - CLOSES A WINDOW - NEEDS HANDLE IN A0
*PRINTIT - PRINTS A STRING IN A0 TERMINATED IN 0 IN CURRENT WINDOW
*SETCURSXY - SETS THE CURSOR COORDS TO D0,D1
*DRAWIT - PRINTS A CHARACTER IN D0 (ASSUMING CURRENT WINDOW)
*PRINTCR - DOES A CR
*PRINTNUM - PRINTS D0.L AS A DECIMAL NUMBER
*C_TO_P_STR - CONVERTS A C STRING TO A PASCAL STRING
**DO NOT CALL ANY OTHER ROUTINES. THEY ARE FOR SUPPORT PURPOSES FOR THE ABOVE
**ROUTINES.
***EQUATES FOLLOW
INITCURSOR: EQU $A850
INITPORT: EQU $A86D
INITGRAF: EQU $A86E
INITFONTS: EQU $A8FE
INITWINDOWS: EQU $A912
INITMENUS: EQU $A930
INITDIALOGS: EQU $A97B
INITRESOURCES: EQU $A995
TEINIT: EQU $A9CC
FLUSHEVENTS: EQU $A032
TEXT_MODE: EQU $A889
GETAPPPARMS: EQU $A9F5
SYSBEEP: EQU $A9C8
NEWWINDOW: EQU $A913
DISPOSEWINDOW: EQU $A914
TEXTFONT: EQU $A887
TEXTSIZE: EQU $A88A
GETFONTINFO: EQU $A88B
MOVETO: EQU $A893
CLOSEWINDOW: EQU $A92D
SETPORT: EQU $A873
BEGINUPDATE: EQU $A922
ENDUPDATE: EQU $A923
DRAWCHAR: EQU $A883
SETRECT: EQU $A8A7
SCROLLRECT: EQU $A8EF
GETNEXTEVENT: EQU $A970
MENUKEY: EQU $A93E
MENUSELECT: EQU $A93D
HILITEMENU: EQU $A938
NEWMENU: EQU $A931
APPENDMENU: EQU $A933
INSERTMENU: EQU $A935
DRAWMENUBAR: EQU $A937
ADDRESMENU: EQU $A94D
GETMENU: EQU $A9BF
PTINRECT: EQU $A8AD
TENEW: EQU $A9D2
TEKEY: EQU $A9DC
TECUT: EQU $A9D6
TECOPY: EQU $A9D5
TEDELETE: EQU $A9D7
TEPASTE: EQU $A9DB
TESETSELECT: EQU $A9D1
TEIDLE: EQU $A9DA
TECLICK: EQU $A9D4
GLOBALTOLOCAL: EQU $A871
TEACTIVATE: EQU $A9D8
TEDEACTIVATE: EQU $A9D9
TEUPDATE: EQU $A9D3
OPEN: EQU $A000
READ: EQU $A002
CLOSE: EQU $A001
GET_EOF: EQU $A011
WRITE: EQU $A003
SET_EOF: EQU $A012
GET_INFO: EQU $A00C
SET_INFO: EQU $A00D
FLUSH: EQU $A013
CREATE: EQU $A008
DELETE: EQU $A009
SETVOL: EQU $A015
CREATERESFILE: EQU $A9B1
OPENRESFILE: EQU $A997
USERESFILE: EQU $A998
CLOSERESFILE: EQU $A99A
RMVERESOURCE: EQU $A9AD
WRITERESOURCE: EQU $A9B0
DETACHRESOURCE: EQU $A992
CHANGEDRESOURCE: EQU $A9AA
UPDATERESFILE: EQU $A999
RELEASERESOURCE: EQU $A9A3
CURRESFILE: EQU $A994
GET1NAMEDRESOURCE: EQU $A820
GET1RESOURCE: EQU $A81F
FINDWINDOW: EQU $A92C
NEWCONTROL: EQU $A954
FINDCONTROL: EQU $A96C
TRACKCONTROL: EQU $A968
SETCTLVALUE: EQU $A963
GETCTLVALUE: EQU $A960
GETCTLMAX: EQU $A962
SETCTLMAX: EQU $A965
STILLDOWN: EQU $A973
GETMOUSE: EQU $A972
TESTCONTROL: EQU $A966
HILITECONTROL: EQU $A95D
TESCROLL: EQU $A9DD
TECALTEXT: EQU $A9D0
DRAWCONTROLS: EQU $A969
TEDISPOSE: EQU $A9CD
GETWTITLE: EQU $A919
DEBUGGER: EQU $A9FF
MAXMEM: EQU $A11D
HLOCK: EQU $A029
SETHANDLESIZE: EQU $A024
MAXAPPLZONE: EQU $A063
SYSTEMTASK: EQU $A9B4
OPENDESKACC: EQU $A9B6
GETITEM: EQU $A946
ALERT: EQU $A985
TICKCOUNT: EQU $A975
SNDPLAY: EQU $A805
PACK3: EQU $A9EA
**********************************INIT MAC***************************************
INIT_MAC: MOVE.L A6,-(SP) SAVE REGISTERS
LEA W_VARS(PC),A6
LEA QDGLOB(PC),A0
LEA 202(A0),A0
MOVE.L A0,-(SP) QDGLOB(202)
DC.W INITGRAF
DC.W INITFONTS
MOVEQ #-1,D0
DC.W FLUSHEVENTS
DC.W INITWINDOWS
DC.W INITMENUS
CLR.L -(SP)
DC.W INITDIALOGS
DC.W TEINIT
DC.W INITCURSOR
BSR INIT_CURSOR
MOVEM.L (SP)+,A6
**END OF MAC INIT**
RTS
DC.B "INIT_MAC",0,0
EVEN
INIT_CURSOR:
MOVE.L A6,-(SP)
LEA W_VARS(PC),A6
MOVE.W #10,CURS_X(A6)
MOVE.W #10,CURS_Y(A6)
MOVE.L (SP)+,A6
RTS
DC.B "INIT_CUR",0,0
EVEN
*****************************wait_key*********************************************
**WAIT FOR A KEY PRESS
WAIT_KEY: MOVEM.L D1-D2/A0-A4,-(SP) *SAVE REGS
WK1: CLR.B -(A7)
MOVE.W #-1,-(SP)
LEA EVENTS_BUFF(PC),A3
MOVE.L A3,-(A7)
DC.W 43376 *GETNEXTEVENT AS A DECIMAL NUMBER
*WOULD BE SMARTER TO EQUATE
MOVE.B (A7)+,D2
TST.B D2
BEQ.S WK1 *WAIT FOR SOMETHING
LEA EVENTS_BUFF(PC),A4
MOVE.W (A4),D2
CMPI.W #3,D2 KEY?
BEQ.S WAIT_KEY_END
CMPI.W #1,D2 MOUSE BUTTON?
BEQ.S WAIT_KEY_END
BRA.S WK1
WAIT_KEY_END:
MOVE.W 4(A4),D0 ACTUAL KEY
MOVEM.L (SP)+,D1-D2/A0-A4 *RESTORE REGS
RTS
EVENTS_BUFF: DS.B 16
DC.B "WAIT_KEY",0,0
EVEN
*******************************MAC LARGE WINDOW**********************************
**OPEN WINDOW NEEDS TITLE IN A0
OPEN_WINDOW: movem.l d0-d7/a1-a6,-(sp) *changes for v1.01
LEA W_VARS(PC),A6
LEA W_TITLE(PC),A1
BSR C_TO_P_STR *CONVERT A0 TO PASCAL IN A1
**SETRECT BOUNDRECT,STARTX,STARTY,ENDX,ENDY
PEA BOUNDRECT(PC)
MOVE.W #100,-(SP)
MOVE.W #100,-(SP)
MOVE.W #500,-(SP)
MOVE.W #400,-(SP)
DC.W SETRECT
**NEWWINDOW
CLR.L -(SP)
CLR.L -(SP)
PEA BOUNDRECT(PC)
PEA W_TITLE(PC)
MOVE.B #1,-(SP)
MOVE.W #4,-(SP)
MOVE.L #-1,-(SP)
MOVE.B #0,-(SP)
MOVE.L #0,-(SP)
DC.W NEWWINDOW
MOVE.L (SP)+,W_HANDLE(A6) *SAVE WINDOW HANDLE
MOVE.L W_HANDLE(A6),-(SP)
DC.W SETPORT
MOVE.W #4,-(SP)
DC.W TEXTFONT
MOVE.W #9,-(SP)
DC.W TEXTSIZE
PEA VIEWRECT(PC)
MOVE.W #4,-(SP)
MOVE.W #10,-(SP)
MOVE.W #399,-(SP)
MOVE.W #299,-(SP)
DC.W SETRECT
MOVE.L W_HANDLE(A6),A0 *RETURN HANDLE
movem.l (sp)+,d0-d7/a1-a6
RTS
DC.B "OPEN_WIN",0,0
EVEN
**CLOSE WINDOW NEEDS HANDLE IN A0
CLOSE_WINDOW: movem.l D0-D7/A1-A6,-(sp)
MOVE.L A0,-(SP)
DC.W DISPOSEWINDOW
movem.l (sp)+,d0-d7/a1-a6
RTS
DC.B "CLOSE_WI",0,0
EVEN
**NEEDS C STRING IN A0
**AND P BUFFER IN A1
**CONVERTS THE C STRING A0 TO A PASCAL STRING IN A1
C_TO_P_STR: move.l d0,-(sp) *preserve d0
CLR.L D0
DO_P_LOOP: MOVE.B (A0)+,D1
BEQ.S CTOPEND
ADDQ.L #1,D0 COUNTER
MOVE.B D1,0(A1,D0.L) *SAVE BYTE IN P STRING
BRA.S DO_P_LOOP
CTOPEND: MOVE.B D0,(A1)
move.l (sp)+,d0
RTS
DC.B "C_TO_P_S",0,0
EVEN
**set the cursor to x,y where d0 is x coord and d1 is y coord
SETCURSXY: movem.l d1-d4/a0-a6,-(sp)
LEA W_VARS(PC),A6
MOVE.W D0,CURS_X(A6)
MOVE.W D1,CURS_Y(A6)
MOVE.W D0,-(SP)
MOVE.W D1,-(SP)
DC.W MOVETO
movem.l (sp)+,d1-d4/a0-a6
RTS
DC.B "SETCURXY",0,0
EVEN
**PRINTIT PRINTS THE STRING IN A0 AT CURS_X,CURS_Y
**THE STRING MUST BE TERMINATED IN 0 BYTE
**THIS IS THE SLOW METHOD OF PRINTING STRINGS.
**THE MAC HAS A PRINTSTRING ROUTINE IN THE TOOLBOX.
PRINTIT: MOVEM.L d0-d1/A0-A6,-(SP)
lea w_vars(pc),a6
MOVE.L A0,-(A7)
MOVE.W CURS_X(A6),-(A7)
MOVE.W CURS_Y(A6),-(A7)
DC.W MOVETO *POSITION CURSOR AT CURS_X,CURS_Y
MOVEA.L (A7)+,A0
**MAIN PRINT LOOP
PR1: MOVE.B (A0)+,D0 *GET CHARACTER
BEQ.S P1END *IF 0 THEN END OF PRINT
CMPI.B #13,D0 *CR?
BEQ.S DO_CR1 *YEP, DO CR
MOVE.L A0,-(A7)
BSR.S DRAWIT *PRINT CHAR
MOVEA.L (A7)+,A0
BRA.S PR1 *LOOP FOR NEXT CHAR
P1END: MOVEM.L (SP)+,d0-d1/A0-A6 *RESTORE REGS
RTS *END OF PRINT
**IF PRINTIT DETECTS CR IT COMES HERE
DO_CR1: MOVE.W #10,CURS_X(A6) *RESET CURS X TO 10
MOVE.W CURS_Y(A6),D1
ADD.W #10,D1 *ADD 10 TO CURS Y
MOVE.W D1,CURS_Y(A6)
MOVE.L A0,-(A7)
MOVE.W D1,-(A7)
MOVE.W CURS_X(A6),-(A7)
DC.W MOVETO
MOVEA.L (A7)+,A0
BRA.S PR1 *BACK TO PRINTIT
**DRAWIT PRINTS A CHARACTER IN D0.
**ALSO STORES THE CHARACTER IN ERROR_BUFFER IF THERES ROOM
**can be called only from printit
DRAWIT: MOVE.W D0,-(A7)
ADDQ.W #6,CURS_X(A6)
DC.W DRAWCHAR
DI_END: RTS
DC.B "PRINTIT "
**print curs prints the character in d0 then a "|" character, then moves the cursor
**coords back 1 x position
PRINT_CURS: movem.l d0/a0-a1/a6,-(sp)
LEA W_VARS(PC),A6
MOVE.W D0,-(SP) *SAVE CHAR
MOVE.W #0,-(sp) ; *AND (1=OR) DRAWING MODE
DC.W TEXT_MODE
SUBQ.W #6,CURS_X(A6) *DELETE OLD CURS
MOVE.W CURS_X(A6),-(SP)
MOVE.W CURS_Y(A6),-(SP)
DC.W MOVETO
DC.W DRAWCHAR *D0 IS ON STACK ALREADY
MOVE.W #"|",-(SP)
DC.W DRAWCHAR
ADD.W #12,CURS_X(A6)
MOVE.W #1,-(SP)
DC.W TEXT_MODE *BACK TO "OR" MODE
MOVEM.L (SP)+,D0/A0-A1/A6
RTS
DC.B "PRINT_CU",0,0
EVEN
**backspace can be called if the delete key is pressed to move the cursor
*8back 1 x position, overwriting the old character
BACKSPACE: movem.l a0-a1/a6,-(sp)
LEA W_VARS(PC),A6
MOVE.W #0,-(A7) ; *AND (1=OR) DRAWING MODE
DC.W TEXT_MODE
SUB.W #6,CURS_X(A6)
MOVE.W CURS_X(A6),-(SP)
MOVE.W CURS_Y(A6),-(SP)
DC.W MOVETO
MOVE.W #" ",-(SP) *DELETE OLD CURS
DC.W DRAWCHAR *D0 IS ON STACK
SUBQ.W #6,CURS_X(A6)
MOVE.W CURS_X(A6),-(SP)
MOVE.W CURS_Y(A6),-(SP)
DC.W MOVETO
MOVE.W #"|",-(SP)
DC.W DRAWCHAR
ADD.W #6,CURS_X(A6)
MOVE.W #1,-(SP)
DC.W TEXT_MODE
movem.l (sp)+,a0-a1/a6
RTS
DC.B "BACKSPAC",0,0
EVEN
**PRINTCR DOES A CR
PRINTCR: MOVE.L A0,-(A7)
LEA CR_TEXT(PC),A0
BSR PRINTIT
MOVEA.L (A7)+,A0
RTS
DC.B "PRINTCR ",0,0
EVEN
**PRINTNUM PRINTS A 32 BIT NUMBER HELD IN D0
**LIMITED TO 1E7
**USES COMMAS FOR SEPERATORS.
PRINTNUM: movem.l d0-d6/a0-a6,-(sp)
LEA W_VARS(PC),A6
**FIND SIZE OF NUMBER TO QUASH LEADING ZEROS
LEA CHAR_BUFF(PC),A0 *WHERE CHARS ARE STORED FOR PRINTING
CMPI.L #$FFFF,D0
BGT.S PRINT_LONG *LONG WORD PRINT
MOVEQ #0,D6
MOVE.W D0,D6
CMPI.L #10,D6
BLT.S DO_UNITS
CMPI.L #100,D6
BLT.S DO_TENS
CMPI.L #1000,D6
BLT.S DO_HUNS
CMPI.L #10000,D6
BLT.S DO_THOU
**NOW DIVIDE BY RELEVANT UNITS
LONG_ENT: DIVS #10000,D6 *GET TENS OF THOUDSANDS
BSR.S CONVERT_N *PRINT NUMBER OF TENS OF THOUSANDS
DO_THOU: DIVS #1000,D6 *GET THOUSANDS
BSR.S CONVERT_N *ETC
MOVE.B #",",0(A0)
BSR PRINTIT *PRINT COMMA BEFORE TENS HUNDREDS
DO_HUNS: DIVS #100,D6
BSR.S CONVERT_N
DO_TENS: DIVS #10,D6
BSR.S CONVERT_N
DO_UNITS: BSR.S CONVERT_N
movem.l (sp)+,d0-d6/a0-a6
RTS
CONVERT_N: ADDI.B #48,D6 *MAKE CHARACTER (ASCII)
MOVE.B D6,0(A0)
BSR PRINTIT *DISPLAY CHARACTER
SWAP D6
ANDI.L #$FFFF,D6
RTS
DC.B "PRINTNUM"
**PRINT LONG PRINT DO.L
**LIMITED TO 1E7 (10MEG)
PRINT_LONG: CMPI.L #9999999,D0
BGT.S NUMB_ERROR
MOVE.L D0,D1
CMPI.L #99999,D0
BLT.S DO_TEN_THOU
CMPI.L #999999,D0
BLT.S DO_HUND_THOU
MOVE.L #1000000,D0 *GET MILLIONS
BSR LONG_DIV *DIVIDE D1.L BY D0.L WITH REMAINDER IN D0
ADDI.B #"0",D1
MOVE.B D1,0(A0)
MOVE.L D0,D6
BSR PRINTIT
MOVE.B #",",0(A0)
BSR PRINTIT
MOVE.L D6,D1
DO_HUND_THOU: MOVE.L #100000,D0 *GET 100 THOUSANDS
BSR LONG_DIV
ADDI.B #"0",D1
MOVE.B D1,0(A0)
MOVE.L D0,D6
BSR PRINTIT
CMPI.L #$FFFF,D6 *<65536?
BLT LONG_ENT *YES, PRINT WORD ROUTINE
MOVE.L D6,D1 *NO,GET 10THOUSANDS
DO_TEN_THOU: MOVE.L #10000,D0
BSR LONG_DIV
ADDI.B #"0",D1
MOVE.B D1,0(A0)
MOVE.L D0,D6
BSR PRINTIT
ANDI.L #$FFFF,D6
BRA DO_THOU
NUMB_ERROR: LEA NUMB_TOO_BIG(PC),A0
BSR PRINTIT
movem.l (sp)+,d0-d6/a0-a6
RTS
dc.b "PRINTLNG",0,0 *MACSBUG LABEL
EVEN
**LONG DIV DIVIDES D1.L BY D0.L
LONG_DIV:
TST.L D0
BNE.S DIV
DIVU.W D0,D1
RTS
DIV:
BPL.S DO_DIV
NEG.L D0
BSR.S DO_DIV
NEG.L D1
RTS
DO_DIV:
TST.L D1
BPL.S DIV_P
NEG.L D1
BSR.S DIV_P
NEG.L D1
NEG.L D0
RTS
DIV_P:
CLR.L D3
CLR.L D2
DIV2:
CMP.L D0,D1
BLS.S DIV1
ADDQ.L #1,D3
LSL.L #1,D0
BMI.S DIV3
BRA.S DIV2
DIV5:
LSL.L #1,D2
LSR.L #1,D0
DIV3:
CMP.L D0,D1
DIV1:
BCS.S DIV4
SUB.L D0,D1
ADDQ.L #1,D2
DIV4:
DBF D3,DIV5
MOVE.L D1,D0
MOVE.L D2,D1
RTS
W_HANDLE: EQU 0
CURS_X: EQU 4
CURS_Y: EQU 6
W_VARS: DS.W 20
W_TITLE: DS.B 100
VIEWRECT: DS.B 8 *VIEW RECTANGLE
BOUNDRECT: DS.B 8
NUMB_TOO_BIG: DC.B "XXXXXXXXXXXX",0
EVEN
CR_TEXT: DC.B 13,0
CHAR_BUFF: DS.B 20
QDGLOB: DS.B 3000
align